﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmMatHangQuyDoi : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private MatHangBOCollection MatHangBOCollection = null;
        private QuyDoiBOCollection QuyDoiBOCollection = null;

        private string COLUMN_FIRST_MatHang = "Ten";
        private string COLUMN_SECOND_MatHang = "SoThuTu";

        private string COLUMN_FIRST_QuyDoi = "Ten";
        private string COLUMN_SECOND_QuyDoi = "HeSo";
        #endregion variant

        //=====================================
        #region form
        public FrmMatHangQuyDoi()
        {
            InitializeComponent();

            this.Load += new EventHandler(FrmMatHangQuyDoi_Load);
            this.FormClosing += new FormClosingEventHandler(FrmMatHangQuyDoi_FormClosing);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnEdit.Click += new EventHandler(btnEdit_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.grdViewMatHang.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);
            this.grdViewQuyDoi.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);

            this.grdViewMatHang.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
            this.grdViewQuyDoi.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
        }

        private void FrmMatHangQuyDoi_Load(object sender, EventArgs e)
        {
            RefreshData();
            SetFormStatus(this.formStatus);

            Utility.InitControl(this.reItemLookUpEditDonViTinh);
            Utility.InitControl(this.reItemLookUpEditHangSanXuat);
            Utility.InitControl(this.reItemLookUpEditNuocSanXuat);
            Utility.InitControl(this.reItemLookUpEditNhomHang);
            this.grdViewMatHang.FocusedRowChanged += new FocusedRowChangedEventHandler(grdViewMatHang_FocusedRowChanged);
        }

        private void FrmMatHangQuyDoi_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void grdViewMatHang_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            FilterMatHang();
        }

        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewMatHang.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void FilterMatHang()
        {
            Session.MatHangID = StaticUtil.GetRealInt(this.grdViewMatHang.GetFocusedRowCellValue("MatHangID"));

            if (this.formStatus == FormStatus.Edit)
                this.grdViewQuyDoi.OptionsView.NewItemRowPosition = (Session.MatHangID != null ? NewItemRowPosition.Bottom : NewItemRowPosition.None);

            if (Session.MatHangID == null)
                this.grdViewQuyDoi.ActiveFilterString = "MatHangID=" + int.MaxValue;
            else
                this.grdViewQuyDoi.ActiveFilterString = "MatHangID=" + Session.MatHangID.Value.ToString();

            this.grdViewQuyDoi.CheckLoaded();
            //SetSttQuyDoi();
        }

        private void RefreshData()
        {

            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reItemLookUpEditDonViTinh, donViTinhBOList, donViTinhBOList.Count);
            
            DanhMucBOList hangSanXuatBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.HangSanXuat);
            Utility.SetData(this.reItemLookUpEditHangSanXuat, hangSanXuatBOList, hangSanXuatBOList.Count);

            DanhMucBOList nuocSanXuatBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.NuocSanXuat);
            Utility.SetData(this.reItemLookUpEditNuocSanXuat, nuocSanXuatBOList, nuocSanXuatBOList.Count);

            NhomHangBOList nhomHangBOList = NhomHangBOList.GetNhomHangBOList();
            Utility.SetData(this.reItemLookUpEditNhomHang, nhomHangBOList, nhomHangBOList.Count);

            #endregion BOList

            this.MatHangBOCollection = MatHangBOCollection.GetMatHangBOCollection();
            this.QuyDoiBOCollection = QuyDoiBOCollection.GetQuyDoiBOCollection();

            this.bdSourceMatHang.DataSource = this.MatHangBOCollection.Clone();
            this.bdSourceQuyDoi.DataSource = this.QuyDoiBOCollection.Clone();



            FilterMatHang();
        }

        private void Edit()
        {
            this.formStatus = FormStatus.Edit;
            SetFormStatus(this.formStatus);
            Session.CloseIndex++;
        }

        private void Save()
        {
            string oldColumn = this.grdViewMatHang.FocusedColumn.FieldName;
            string newColumn = (oldColumn == this.COLUMN_FIRST_MatHang ? this.COLUMN_SECOND_MatHang : this.COLUMN_FIRST_MatHang);
            this.grdViewMatHang.FocusedColumn = this.grdViewMatHang.Columns[newColumn];

            string oldColumn2 = this.grdViewQuyDoi.FocusedColumn.FieldName;
            string newColumn2 = (oldColumn2 == this.COLUMN_FIRST_QuyDoi ? this.COLUMN_SECOND_QuyDoi : this.COLUMN_FIRST_QuyDoi);
            this.grdViewQuyDoi.FocusedColumn = this.grdViewQuyDoi.Columns[newColumn2];

            this.bdSourceMatHang.EndEdit();
            this.bdSourceQuyDoi.EndEdit();

            this.MatHangBOCollection = (MatHangBOCollection)this.bdSourceMatHang.DataSource;
            this.QuyDoiBOCollection = (QuyDoiBOCollection)this.bdSourceQuyDoi.DataSource;

            this.MatHangBOCollection = this.MatHangBOCollection.Save();

            //int i = 0;
            //int length = this.QuyDoiBOCollection.Count;
            //int oldID = 0;
            //for (i = 0; i < length; i++)
            //{
            //    oldID = this.QuyDoiBOCollection[i].MatHangID.Value;
            //    if (this.MatHangBOCollection.hstID.ContainsKey(oldID))
            //        this.QuyDoiBOCollection[i].MatHangID = (int)this.MatHangBOCollection.hstID[oldID];
            //    else //không có chứa trong danh sách mới tức ngạch đã bị xóa
            //        this.QuyDoiBOCollection[i].DELE = true;
            //}
            this.QuyDoiBOCollection = this.QuyDoiBOCollection.Save();


            this.bdSourceMatHang.DataSource = this.MatHangBOCollection.Clone();
            this.bdSourceQuyDoi.DataSource = this.QuyDoiBOCollection.Clone();

            this.grdViewMatHang.FocusedColumn = this.grdViewMatHang.Columns[oldColumn];
            this.grdViewQuyDoi.FocusedColumn = this.grdViewQuyDoi.Columns[oldColumn2];

            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            FilterMatHang();
        }

        private void Cancel()
        {
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            this.bdSourceMatHang.DataSource = this.MatHangBOCollection.Clone();
            this.bdSourceQuyDoi.DataSource = this.QuyDoiBOCollection.Clone();

            FilterMatHang();
        }

        private void SetFormStatus(FormStatus formStatus)
        {
            bool edit = (formStatus != FormStatus.View);
            this.allowCloseForm = !edit;

            MyStyle.SetGridStyle(this.grdViewMatHang, edit);
            MyStyle.SetGridStyle(this.grdViewQuyDoi, edit);

            this.btnEdit.Visible = !edit;
            this.btnClose.Visible = !edit;
            this.btnRefresh.Visible = !edit;

            this.btnSave.Visible = edit;
            this.btnCancel.Visible = edit;

            this.colDELE_MatHang.Visible = edit;
            foreach (GridColumn col in this.grdViewMatHang.Columns)
            {
                if (col.Visible)
                    col.OptionsColumn.AllowEdit = edit;
            }

            this.colDELE_QuyDoi.Visible = edit;
            foreach (GridColumn col in this.grdViewQuyDoi.Columns)
            {
                if (col.Visible)
                    col.OptionsColumn.AllowEdit = edit;
            }


            this.grdViewMatHang.OptionsView.NewItemRowPosition = (edit ? NewItemRowPosition.Bottom : NewItemRowPosition.None);

            if (edit)
                this.grdViewQuyDoi.OptionsView.NewItemRowPosition = (Session.MatHangID != null ? NewItemRowPosition.Bottom : NewItemRowPosition.None);
            else
                this.grdViewQuyDoi.OptionsView.NewItemRowPosition = NewItemRowPosition.None;

            this.grdViewMatHang.Focus();

            if (edit)
            {
                string focusColumn = this.grdViewMatHang.FocusedColumn.FieldName;

                if (string.IsNullOrEmpty(focusColumn))
                    focusColumn = this.COLUMN_FIRST_MatHang;
                else
                    if (focusColumn == "DELE")
                        focusColumn = this.COLUMN_FIRST_MatHang;

                if (this.grdViewMatHang.RowCount == 1)
                    this.grdViewMatHang.FocusedRowHandle = int.MinValue + 1;

                this.grdViewMatHang.FocusedColumn = this.grdViewMatHang.Columns[focusColumn];
                this.grdViewMatHang.ShowEditor();
            }
        }

        //private void SetSttQuyDoi()
        //{
        //    if (this.formStatus == FormStatus.Edit)
        //        this.bdSourceQuyDoi.EndEdit();

        //    int stt = 0;
        //    int length = this.grdViewQuyDoi.RowCount;
        //    int index = 0;
        //    for (int i = 0; i < length; i++)
        //    {
        //        index = this.grdViewQuyDoi.GetVisibleRowHandle(i);
        //        if (index >= 0)
        //            this.grdViewQuyDoi.SetRowCellValue(i, "SoThuTu", ++stt);
        //    }

        //    ((QuyDoiBOCollection)this.bdSourceQuyDoi.DataSource).SttIndex = stt;
        //}
        #endregion function
    }
}